package com.example.monitoringsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.monitoringsystem.bean.Doctor;
import com.example.monitoringsystem.bean.Patient;
import com.example.monitoringsystem.bean.User;
import com.example.monitoringsystem.currentUser.StaticUser;
import com.example.monitoringsystem.mapper.DoctorMapper;
import com.example.monitoringsystem.mapper.PatientMapper;
import com.example.monitoringsystem.mapper.UserMapper;
import com.example.monitoringsystem.service.NewUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class NewUserServiceImpl implements NewUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    //返回当前用户信息
    @Override
    public User findUser() {
        User currentUser=new User ();
            Subject subject = SecurityUtils.getSubject ();
           currentUser = (User) subject.getPrincipal ();
       if(currentUser==null)
       {
           System.out.println ("用户为空");
       }
        return currentUser;
    }
    //显示病人的医生信息
    @Override
    public Doctor findDoctorByPatient(int doctorId) {
        Doctor doctor=new Doctor ();
        QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<Doctor> ();
        doctorQueryWrapper.eq ( "doctor_id", doctorId );
        try {
          doctor  = doctorMapper.selectOne ( doctorQueryWrapper );
        } catch (Exception e) {
            doctor=null;
        }
        return doctor;
    }
    //病人的病人信息
    @Override
    public Patient findPatientById(int patientId) {
        Patient patient=new Patient ();
        QueryWrapper<Patient> patientQueryWrapper = new QueryWrapper<Patient> ();
        patientQueryWrapper .eq ( "patient_id", patientId );
        try {
            patient = patientMapper.selectOne ( patientQueryWrapper  );
        } catch (Exception e) {
          patient=null;
        }
        return patient;
    }
//医生的医生信息
    @Override
    public Doctor findDoctor(int doctorId) {
      Doctor doctor=new Doctor ();
        try {
            QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<Doctor> ();
            doctorQueryWrapper.eq ( "doctor_id", doctorId );
          doctor =doctorMapper.selectOne ( doctorQueryWrapper );
        } catch (Exception e) {
          doctor=null;
        }
        return doctor;
    }
//修改名字
    @Override
    public boolean updateName(String name,int id) {
        try {
            UpdateWrapper<User> wrapper = new UpdateWrapper<> ();
            wrapper.set ( "username", name )
                    .eq ( "id",id );
            return true;
        } catch (Exception e) {
           return  false;
        }

    }
    //修改密码
    @Override
    public boolean updatePassword(String password,int id) {
//      QueryWrapper<User> userQueryWrapper=new QueryWrapper<> ();
//      userQueryWrapper.eq ( "id", )
//        User user=userMapper.selectOne (  )
        UpdateWrapper<User> wrapper = new UpdateWrapper<> ();
        wrapper.set ( "password", password )
                .eq ( "id", id );
        return  true;
    }
    //修改邮件
    @Override
    public boolean updateEmail(String email,int id) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<> ();
        wrapper.set ( "email", email )
                .eq ( "id",id );
        return true;
    }
    //修改年龄
    @Override
    public boolean updateAge(int age,int id) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<> ();
        wrapper.set ( "age", age )
                .eq ( "id",id );
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        userMapper.updateById ( user );
       return  true;
    }
}
